<?php

Class Siswa extends CI_Controller {

//constructor
    function __construct() {
        parent::__construct();
        $this->load->model('model_user');
        if ($this->model_user->auth() == 0 || $this->model_user->auth() !== 1) {
            redirect(
                    'login/index');
        }else{
            if($this->session->userdata('type') == 'guru'){
                redirect('guru');
            }else if($this->session->userdata('type') == 'admin'){
                redirect('admin');
            }
        }
        $this->load->model('Model_soal');
        $this->load->model('model_nilai');
        $this->load->library('pagination');
    }

    function index() {
        $data['nama'] = $this->session->userdata['nama'];
        $data['main_content'] = 'siswa/index';
        $this->load->view('includes/template', $data);
    }

    function kerjakan() {
        $id = $this->uri->segment(3);
        $data['result'] = $this->Model_soal->getEditSoal($id);
        $cek = $this->Model_soal->cekSelesai($id);
        
        $selesai = $cek->row();
        if (empty($id) or count($data['result']) == 0) {
            redirect ('siswa/test');

        } 
        else if ($cek->num_rows() == 1) {
            if($selesai->is_selesai == 1){
                $data['nama'] = 'Maaf '.$this->session->userdata('nama').' telah mengerjakan soal ini';
                $data['main_content'] = 'siswa/indes';
                $this->load->view('includes/template', $data);
            }
            else{
            $data['waktu'] = $this->Model_soal->getWaktu($id);
            $perpage = 1; //tentukan jumlah data per halaman
            //untuk konfigurasi pagination
            $segment = $this->uri->segment(4);
            $config = array(
                'base_url' => base_url() . 'siswa/kerjakan/' . $id . '',
                'total_rows' => $this->Model_soal->itungdetsoal($id),
                'per_page' => $perpage,
                'num_links' => 1,
                'uri_segment' => 4,
                'full_tag_open' => '<div class="pagination pagination-centered paging"><ul>',
                'full_tag_close' => '</ul></div>',
                'num_tag_open' => '<li class="jawab">',
                'num_tag_close' => '</li>',
                'prev_tag_open' => '<li class="jawab">',
                'prev_tag_close' => '</li>',
                'next_tag_open' => '<li class="jawab">',
                'next_tag_close' => '</li>',
                'last_tag_open' => '<li>',
                'last_tag_close' => '</li>',
                'first_tag_open' => '<li>',
                'first_tag_close' => '</li>',
                'cur_tag_open' => '<li class="active"><span>',
                'cur_tag_close' => '</span></li>',
                'first_link' => false,
                'last_link' => false
            );    //inisialisasi pagination dn config di atas
            $this->pagination->initialize($config);
            $data['row'] = $this->Model_soal->getKerjakanSoal($id, $perpage, $segment);
            $data['jawab'] = $this->Model_soal->cekjawab($data['row']->id);
            $data['jumops'] = $this->Model_soal->countjumsoal($id);
            $data['main_content'] = 'siswa/kerjakan';

            $this->load->view('includes/template', $data);
                
            }
            
        } 
        else {
            $this->Model_soal->setNilai($id);
            $this->Model_soal->setHasil($id);
            $data['waktu'] = $this->Model_soal->getWaktu($id);
            $perpage = 1; //tentukan jumlah data per halaman
            //untuk konfigurasi pagination
            $segment = $this->uri->segment(4);
            $config = array(
                'base_url' => base_url() . 'siswa/kerjakan/' . $id . '',
                'total_rows' => $this->Model_soal->itungdetsoal($id),
                'per_page' => $perpage,
                'num_links' => 1,
                'uri_segment' => 4,
                'full_tag_open' => '<div class="pagination paging"><ul>',
                'full_tag_close' => '</ul></div>',
                'num_tag_open' => '<li class="jawab">',
                'num_tag_close' => '</li>',
                'prev_tag_open' => '<li class="jawab">',
                'prev_tag_close' => '</li>',
                'next_tag_open' => '<li class="jawab">',
                'next_tag_close' => '</li>',
                'last_tag_open' => '<li>',
                'last_tag_close' => '</li>',
                'first_tag_open' => '<li>',
                'first_tag_close' => '</li>',
                'cur_tag_open' => '<li class="active"><span>',
                'cur_tag_close' => '</span></li>',
                'first_link' => false,
                'last_link' => false
            );    //inisialisasi pagination dn config di atas
            $this->pagination->initialize($config);
            $data['row'] = $this->Model_soal->getKerjakanSoal($id, $perpage, $segment);
            $data['jawab'] = $this->Model_soal->cekjawab($data['row']->id);
            $data['jumops'] = $this->Model_soal->countjumsoal($id);
            
            $data['main_content'] = 'siswa/kerjakan';

            $this->load->view('includes/template', $data);
        }
    }

    function test() {
        $this->load->model('Model_soal');
        $data['rows'] = $this->Model_soal->getSoalSiswa($this->session->userdata('kelas'));
        $data['main_content'] = 'siswa/test';
        $this->load->view('includes/template', $data);
    }

    function jawab() {
        if ($this->input->post('ajax') == '1') {
            if ($this->input->post('jawaban') != '') {
                if ($this->input->post('id_jawab') != '') {
                    $this->Model_soal->updateJawab();
                } else {
                    $this->Model_soal->jawab();
                }
            }
        }
    }

    function hentikan() {
        if ($this->input->post('ajax') == 1) {
            $point = $this->Model_soal->point($this->input->post('id_soal'));
            $cek = $this->Model_soal->cek($this->input->post('id_soal'), $this->session->userdata('id'));
            $betul = 0;
            $salah = 0;
            foreach ($cek as $row) {
                if ($row->kunci == $row->jawaban) {
                    $betul = $betul + 1;
                } else {
                    $salah = $salah + 1;
                }
            }
            $nilai = $point->point * $betul;
            $this->Model_soal->simpanNilai($this->input->post('id_soal'), $this->session->userdata('id'), $betul, $salah, $nilai);
        }
    }
    
    function nilai() {
        $data['nilai'] = $this->model_nilai->nilaiSiswa($this->session->userdata('id'));
        $data['main_content'] = 'siswa/nilai';
        $this->load->view('includes/template', $data);
    }

}

?>